/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class DateUtil {

    static Logger logger = Logger.getLogger(DateUtil.class.getName());

    public static String getYesterdayDate() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(new Date());//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
            String yesterdayDate = df.format(calendar.getTime());
            return yesterdayDate;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return "";
        }

    }

    public static String getToDayDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String todayDate = df.format(new Date());
        return todayDate;
    }

    public static String getTheDayBeforeDate() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(new Date());//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -2);  //设置为前一天
            String yesterdayDate = df.format(calendar.getTime());
            return yesterdayDate;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return "";
        }
    }

    public static long getTimeStamp() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String todayDate = format.format(new Date());
        Date date = format.parse(todayDate);
//        System.out.println(todayDate);
//        System.out.print("Format To times:" + date.getTime());
        return date.getTime();
    }
}
